/*
 * HUD - Heads Up Display (various overlays for playability+debugging)
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#if flash9
import flash.geom.Point;
import flash.display.Sprite;
import flash.display.Bitmap;
import flash3f.Images;
import flash.Lib;
import flash3f.InputHandler;
import flash3f.BitFont;
#else neko
import lib3f.Point;
import neko3f.InputHandler;
#end
import lib3f.IntPoint;
import IsoMetrics;

class HUD extends Control
// draw the cursor, debug information, etc..
{
	#if flash9
	public static var cursor : IsoBitmap;
	public static var overlays : Sprite;
	#end

	public static var ctx : Int;
	public static var cty : Int;
	
	public static function start()
	{
		#if flash9
		cursor = new IsoBitmap(Images.getdata(0xFF00FF));
		#end
		setcursor(250,150);
		#if flash9
		Lib.current.addChild(cursor);
		overlays = new Sprite();
		Lib.current.addChild(overlays);
		#end
	}

	public static function end()
	{
		#if flash9
		Lib.current.removeChild(cursor);
		Lib.current.removeChild(overlays);
		#end
	}
		
	public static function up()
	{
		setcursor(InputHandler.cursx, InputHandler.cursy);
		#if flash9
		
		// clear overlays

		if (GameFace.listening == false) // if we are not playing a game...
		{
			overlays.visible = false;
			cursor.visible = false;
		}
		else
		{
			overlays.visible = true;
			cursor.visible = true;	

			for (n in 0...overlays.numChildren)
			{
				overlays.removeChildAt(0);
			}		

			// draw overlays
			wavecounter();
			scanner();
		}
		#end
		
	}

	//public static var pathover : Bitmap;
	
	/*public static function calcpathover()
	{
		for (entry in 0...3)
		{
			for (n in Creep.shore[entry])
			{				
				var cur : GameTile = n;

				while (cur.waterpaths[entry]!=null)
					{
						var nb : IsoBitmap = new IsoBitmap(Images.getdata(0xFF00FF));
						var npc : Point = IsoMetrics.tiletopix_xy(cur.x, cur.y);
						nb.x = npc.x;
						nb.y = npc.y;
						Lib.current.addChild(nb);		
						cur = cur.waterpaths[entry];
					}
			}
		}	
	}*/
	
	//public static var count : Int = 0;
	
	public static function setcursor(x : Float, y : Float)
	// take the mouse coordinates(or other recording coordinates)
	// and convert them first to tile coordinates, and then to
	// the tile cursor's display coordinates.
	{
		var np : IntPoint = IsoMetrics.pixtotile_xy(x,y);
		var npc : Point = IsoMetrics.tiletopix_point(np);
		ctx = np.x;
		cty = np.y;
		#if flash9
		cursor.x = npc.x;
		cursor.y = npc.y;
		#end
		
	}

	#if flash9
	public static var waveimg : Bitmap = new Bitmap();
	public static var lastwave : Int = -999;
	
	public static function wavecounter()
	// write the current wave
	{

		if (Game.cur.fight.wave!=lastwave)
		{
			lastwave = Game.cur.fight.wave;
			waveimg.bitmapData = Fonts.verdanaitalic.gen(["Wave: "+lastwave]);
			waveimg.x = 2;
			waveimg.y = 425-22;
		}
		
		overlays.addChild(waveimg);
	
	}
	
	public static var scannerimg : Bitmap = new Bitmap();
	
	public static function scanner()
	{
		
		if (Graph.inbounds(ctx,cty, Game.cur.map) && Game.cur.map[ctx][cty].fort!=null)
		{		
			var ourfort = Game.cur.map[ctx][cty].fort;
			var strdata : String = "";
			if (ourfort.mystyle==1) // wall
			{
				strdata+="Wall: ";
				strdata+=Std.string(ourfort.health);
				strdata+="/15";
			}
			else if (ourfort.mystyle==2) // castle
			{
				strdata+="Castle: ";
				strdata+=Std.string(ourfort.health);
				strdata+="/100";
			}
			else if (ourfort.mystyle==3) // tower
			{
				strdata+="Tower: ";
				strdata+=Std.string(ourfort.health);
				strdata+="/50";			
			}
			if (ourfort.shooter!=null && ourfort.shooter.shots_fired>0)
			{
				strdata+="  ";
				strdata+=Std.string(Std.int(1000*ourfort.shooter.shots_hit/ourfort.shooter.shots_fired)/10.0);
				strdata+="% accuracy";
			}
			scannerimg.bitmapData = Fonts.verdanaitalic.gen([strdata]);
			scannerimg.x = 680-scannerimg.bitmapData.width-2;
			scannerimg.y = 2;
			overlays.addChild(scannerimg);
		}
	}
	
	#end
	
	////////// draw functions
	
	//public static var dtxt : Bitmap = new Bitmap();
	/*public static function debugtext(msg : Array<Dynamic>)
	{
		dtxt.bitmapData = BitFont.gen(msg);
		dtxt.x=24;
		dtxt.y=16;
		overlays.addChild(dtxt);
	}*/
	
	/*public static function tiledraw(tx : Int, ty : Int, id : Int)
	{	
		var nb : IsoBitmap = new IsoBitmap(Images.getdata(id));
		var npc : Point = IsoMetrics.tiletopix_xy(tx, ty);
		nb.x = npc.x;
		nb.y = npc.y;
		overlays.addChild(nb);		
	}*/
	
	////////// debugging overlays
	
	/*public static function showterritory()
	{
		if (Graph.inbounds(ctx,cty,Game.cur.map))
			trace(Game.cur.map[ctx][cty].territory);
		else
			trace("oob");
	}*/

	/*public static function showshore()
	{
		//for (z in 0...Creep.preferredshore.length)
		for (n in Creep.shore[0])
		{
			tiledraw(n.x, n.y, 0xEEEE01);
		}
		if (Creep.preferredshore.length>0)
		{
		for (n in Creep.preferredshore[0])
		{
			tiledraw(n.x, n.y, 0xFD00FF);
		}
		}
	}*/
	
	/*public static function showpath(which : Int)
	{
		if (Graph.inbounds(ctx,cty,Game.cur.map) && Game.cur.map[ctx][cty].waterpaths[which]!=null)
		{
			tiledraw(Game.cur.map[ctx][cty].waterpaths[which].x, Game.cur.map[ctx][cty].waterpaths[which].y, 0x000001);
		}
	}*/
	
	/*public static function showlinks()
	// show links between nodes
	{
		if (Graph.inbounds(ctx,cty,Game.cur.map))
		{
			var ct : GameTile = Game.cur.map[ctx][cty];
			for (l in ct.links)
			{
				var nb : IsoBitmap = new IsoBitmap(Images.getdata(0xFF00FF));
				var npc : Point = IsoMetrics.tiletopix_xy(l.x, l.y);
				nb.x = npc.x;
				nb.y = npc.y;
				overlays.addChild(nb);
			}
		}	
	}*/
	
}
